Skip to content

Conversation

@lucasly-ba
Copy link
Contributor

@lucasly-ba lucasly-ba commented Nov 13, 2025

Fixes #4260
on top of PR #4283 and PR #4331
Used the HIR default visitor for AssignExpr, added a map, to have all the unused assignments and attached some methods to it.
Since

a = 5

is an HIR::PathInExpression we cannot visit the left expr of an assignment, because it would be considerered used. So I also added a walk_right to only visit the right expr.
( if we have a = b, we want to visit b)

@lucasly-ba lucasly-ba changed the title Implement unused variable checker on HIR Implement unused assignments checker on HIR Nov 13, 2025
@lucasly-ba lucasly-ba changed the title Implement unused assignments checker on HIR Implement unused assignments lints on HIR Nov 13, 2025
@lucasly-ba lucasly-ba force-pushed the 4260-unused-assignments branch from 4712032 to 344b6eb Compare November 13, 2025 18:06
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really good work, well done!!!

@lucasly-ba lucasly-ba force-pushed the 4260-unused-assignments branch 16 times, most recently from 95fc4c2 to 9353bdf Compare November 18, 2025 12:42
@P-E-P P-E-P mentioned this pull request Nov 24, 2025
@lucasly-ba lucasly-ba force-pushed the 4260-unused-assignments branch 3 times, most recently from 02a2dd5 to 6f54a63 Compare December 10, 2025 12:19
@lucasly-ba lucasly-ba force-pushed the 4260-unused-assignments branch 3 times, most recently from 9b3255d to 044e865 Compare December 17, 2025 13:44
gcc/rust/ChangeLog:

	* checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker):
	Implement unused assignments warning.
	(UnusedVarChecker::go): Remove unique pointer unused var context.
	(UnusedVarChecker::visit): Visit AssignExpr in HIR default visitor.
	* checks/lints/unused-var/rust-unused-var-checker.h: Add visit method.
	* checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector):
	Collect warnings for assignments.
	(UnusedVarCollector::visit): Visit AssignExpr in HIR default visitor.
	* checks/lints/unused-var/rust-unused-var-collector.h: Add visit method.
	* checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_assign):
	Add assignment in map.
	(UnusedVarContext::remove_assign): Remove assignment in map.
	(UnusedVarContext::is_variable_assigned): Check if a variable is assigned.
	* checks/lints/unused-var/rust-unused-var-context.h: Add a map to stock assignments.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4260_0.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog:

	* checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker::visit):
	Change unused name warning to unused variable warning.
	* checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector::visit):
	Remove useless methods.
	* checks/lints/unused-var/rust-unused-var-collector.h: Same here.
	* checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_variable):
	Add used variables to set.
	(UnusedVarContext::mark_used): Remove method.
	(UnusedVarContext::is_variable_used):
	Check if the set contains the hir id linked to a variable.
	(UnusedVarContext::as_string): Refactor method for new set.
	* checks/lints/unused-var/rust-unused-var-context.h: Refactor methods.
	* lang.opt: Change description for unused check flag.

gcc/testsuite/ChangeLog:

	* rust/compile/static_item_0.rs: Modify warning output.
	* rust/compile/template_function_0.rs: Modify warning output.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog:

	* Make-lang.in: Compile the right files.
	* checks/lints/unused-var/rust-unused-var-checker.cc: Move to...
	* checks/lints/unused/rust-unused-checker.cc: ...here.
	* checks/lints/unused-var/rust-unused-var-checker.h: Move to...
	* checks/lints/unused/rust-unused-checker.h: ...here.
	* checks/lints/unused-var/rust-unused-var-collector.cc: Move to...
	* checks/lints/unused/rust-unused-collector.cc: ...here.
	* checks/lints/unused-var/rust-unused-var-collector.h: Move to...
	* checks/lints/unused/rust-unused-collector.h: ...here.
	* checks/lints/unused-var/rust-unused-var-context.cc: Move to...
	* checks/lints/unused/rust-unused-context.cc: ...here.
	* checks/lints/unused-var/rust-unused-var-context.h: Move to...
	* checks/lints/unused/rust-unused-context.h: ...here.
	* rust-session-manager.cc (Session::compile_crate): Call the right method.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
@lucasly-ba lucasly-ba force-pushed the 4260-unused-assignments branch from 044e865 to 10403d9 Compare December 18, 2025 15:53
@P-E-P P-E-P enabled auto-merge December 18, 2025 16:31
@P-E-P P-E-P added this pull request to the merge queue Dec 18, 2025
Merged via the queue into Rust-GCC:master with commit fea14cb Dec 18, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unused assignments lint not implemented

4 participants